package cn.afterturn.easypoi.word.parse;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.entity.bookmark.ImageValueEntityVO;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

import static cn.afterturn.easypoi.util.PoiElUtil.END_STR;
import static cn.afterturn.easypoi.util.PoiElUtil.START_STR;

public class ImageParse {
    static BASE64Encoder encoder = new BASE64Encoder();
    static BASE64Decoder decoder = new BASE64Decoder();
    public static ImageEntity parseImage(String currentText) throws Exception{
            currentText = currentText.substring(currentText.indexOf(START_STR) + 2, currentText.indexOf(END_STR));
            currentText=currentText.substring(currentText.indexOf("(")+1,currentText.indexOf(")"));
            currentText="{"+currentText+"}";
            System.out.println("打印出图片中的字段内容"+currentText+"\r\n");
            currentText = currentText.replace("“", "\"").replace("”", "\"");
            JSONObject jsonObject2 =JSONObject.parseObject(currentText);
            ImageEntity imageEntity = new ImageEntity();
            /*value.setWidth(500);
            value.setHeight(200);
            String localaddress="d:/testCode.png";*/

            imageEntity.setHeight(Integer.parseInt(jsonObject2.get("高度").toString()));
            imageEntity.setWidth(Integer.parseInt(jsonObject2.get("宽度").toString()));
            String localaddress=jsonObject2.get("本地地址").toString();
            /*String[] imagArray=currentText.split(",");
            for(int i=0;i<imagArray.length;i++){
                String[] keyvalue=imagArray[i].split(":");
                if(keyvalue[0].toString().trim().contains("高度")){
                    //替换这个字符串有问题，应采用正值表达式，把数字取出来-----

                    value.setHeight(Integer.parseInt(keyvalue[1].replaceAll("\"","\\\\\"")));
                }
                if(keyvalue[0].toString().trim().contains("宽度")){
                    value.setWidth(Integer.parseInt(keyvalue[1]));

                }
                if(keyvalue[0].toString().trim().contains("本地地址")){
                    localaddress =keyvalue[1];

                }
            }
                //JSONObject jsonObject1 = JSONObject.parseObject(currentText);

*/
            File file = new File(localaddress);
            if(file.exists()){
                imageEntity.setUrl(ImageEntity.URL);
                imageEntity.setData(getBytes(localaddress));

            }else {
                System.out.println("这个不是文件，是网络过来的流");
                try {
                    imageEntity.setUrl(ImageEntity.Data);
                    byte[] bytes1 = decoder.decodeBuffer(localaddress);
                    imageEntity.setData(bytes1);
                    /*ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
                    BufferedImage bi1 =ImageIO.read(bais);
                    File w2 = new File("d:/hsjkmeinv.bmp");//可以是jpg,png格式
                    ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动*/
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
          return imageEntity;
    }

    private static byte[] getBytes(String filePath) throws Exception{
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
    /*
    * 从网上来的数据
    * */
    public static ImageEntity parseImage(String currentText,String bookmarkValue) throws Exception{
         ImageEntity imageEntity = new ImageEntity();

         imageEntity.setUrl(ImageEntity.Data);
         byte[] bytes1 = decoder.decodeBuffer(bookmarkValue);
         imageEntity.setData(bytes1);

        return imageEntity;
    }
    /*
     * 从网上来的数据
     * */
    public static ImageEntity parseImageExt(String bookmarkValue) throws Exception{
        ImageEntity imageEntity = new ImageEntity();

        imageEntity.setUrl(ImageEntity.Data);
        byte[] bytes1 = decoder.decodeBuffer(bookmarkValue);
        imageEntity.setData(bytes1);

        return imageEntity;
    }
    /*
     * 从网上来的数据
     * */
    public static ImageEntity parseImageExt(ImageValueEntityVO  imageValueEntityVO) throws Exception{
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setHeight(imageValueEntityVO.getHeight());
        imageEntity.setWidth(imageValueEntityVO.getHeight());
        imageEntity.setUrl(ImageEntity.Data);
        byte[] bytes1 = decoder.decodeBuffer(imageValueEntityVO.getDataofimagestring());
        imageEntity.setData(bytes1);

        return imageEntity;
    }
    public static ImageEntity parseImage(ImageValueEntityVO imageValueEntityVO) throws Exception{
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setWidth(imageValueEntityVO.getWidth());
        imageEntity.setHeight(imageValueEntityVO.getHeight());
        imageEntity.setUrl(ImageEntity.Data);
        byte[] bytes1 = decoder.decodeBuffer(imageValueEntityVO.getDataofimagestring());
        imageEntity.setData(bytes1);

        return imageEntity;
    }
}
